<template>

  <div v-if="this.handle=== 'add2'" app-container name="addPage">
    <h1>申请资料填写</h1>
    <el-descriptions title="用户信息" style="margin:auto;width:90%">
      <el-descriptions-item label="贷款编号">
        <span>{{this.newFeedLoanNum}}</span>
      </el-descriptions-item>
      <el-descriptions-item label="渠道名称">
        <span>{{this.queryParams.channelName}}</span>
      </el-descriptions-item>

      <el-descriptions-item label="渠道产品">
        <span>{{this.newFeedProductName}}</span>

      </el-descriptions-item>
      <el-descriptions-item label="客户姓名">
        <span>{{this.queryParams.clientName}}</span>

      </el-descriptions-item>
      <el-descriptions-item label="手机号">
        <span>{{this.queryParams.phone}}</span>
      </el-descriptions-item>
      <el-descriptions-item label="身份证号码">
        <span>{{this.queryParams.citizenIdentity}}</span>
      </el-descriptions-item>
    </el-descriptions>
    <el-collapse class="dataBox" v-for="(one, index) in allData" v-model="activeNames" @change="handleChange">

      <el-collapse-item :title="one.title" name="1">
        <template slot="title">
          <p class="title">{{ one.title }}</p>
        </template>
        <div v-for="oneone in one.viewDetails" id="context">
          <el-form :ref="oneone" :model="oneone" label-width="auto" label-position="left" :inline="true">
            <el-form-item v-if="oneone.fieldType != 'select'" :label="oneone.fieldName" :prop="oneone.field">
              <div v-if="oneone.fieldName.includes('正面')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess0" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl0" :src="imageUrl0" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else-if="oneone.fieldName.includes('反面')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess1" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else-if="oneone.fieldName.includes('手持')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess2" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else>
                <el-input v-model="oneone.value" :type="oneone.fieldType" :value="oneone.value"
                  :class="oneone.fieldClass" />
              </div>

            </el-form-item>

            <el-form-item v-else-if="oneone.fieldType == 'select'" :label="oneone.fieldName" :prop="oneone.field">
              <el-select v-model="oneone.value" placeholder="">
                <el-option v-for="dict in dict.type.user_personal_national"
                  v-if="oneone.dictType == 'dict.type.user_personal_national'" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>

          </el-form>
        </div>
      </el-collapse-item>

    </el-collapse>

    <el-row :gutter="10" class="btn">

      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="commit" plain size="max" @click="saveUerInfo($event)">
          提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="save" plain size="max" @click="saveUerInfo($event)">
          保存但不提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="giveUp" plain size="max" @click="saveUerInfo($event)">
          放弃申请</el-button>
      </el-col>
    </el-row>
  </div>
  <div app-container v-else-if="this.handle=== 'viewDetails' " name="DetailsPage">
    <el-row type="flex" justify="center">
      <el-col :span="21">
        <div align="center" style="background-color: lightgray;height: 60px;">
          <div>
            <span id="mainTitle">订单详情</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row class="mb8" justify="center" type="flex">
      <el-col :span="20">
        <el-descriptions title="申请信息" name="1" size="medium" :column="3" class="con">
          <el-descriptions-item label="申请编号">
            <span v-text="info.orderNumber" />
          </el-descriptions-item>
          <el-descriptions-item label="申请状态">
            <span v-text="info.applicantStatus" />
          </el-descriptions-item>
          <el-descriptions-item label="产品渠道名">
            <span v-text="info.proChan" />
          </el-descriptions-item>
          <el-descriptions-item label="客户姓名">
            <span v-text="info.clientName" />
          </el-descriptions-item>
          <el-descriptions-item label="手机号码">
            <span v-text="info.phone" />
          </el-descriptions-item>
          <el-descriptions-item label="身份证号">
            <span v-text="info.citizenIdentity" />
          </el-descriptions-item>
          <el-descriptions-item label="渠道">
            <span v-text="info.channelName" />
          </el-descriptions-item>
        </el-descriptions>
        <hr>
      </el-col>
    </el-row>
    <el-row class="mb8" justify="center" type="flex">
      <el-col :span="20">
        <el-descriptions title="借款信息" name="1" size="medium" :column="3" class="con">
          <template slot="title">
            <p class="title">借款信息</p>
          </template>
          <el-descriptions-item label="贷款金额">
            <span v-text="info.loanAmount" />
          </el-descriptions-item>
          <el-descriptions-item label="还款方式">
            <span v-text="info.paymentStatusValue" />
          </el-descriptions-item>
          <el-descriptions-item label="分期期数">
            <span v-text="info.periods" />
          </el-descriptions-item>
          <el-descriptions-item label="贷款用途">
            <span v-text="info.usage" />
          </el-descriptions-item>
          <el-descriptions-item label="还款来源">
            <span v-text="info.paybackOrigin" />
          </el-descriptions-item>
          <el-descriptions-item label="还款保障">
            <span v-text="info.paybackGuarantee" />
          </el-descriptions-item>
        </el-descriptions>
        <hr>
      </el-col>
    </el-row>
    <el-row class="mb8" justify="center" type="flex">
      <el-col :span="20">
        <div v-for="(one, index) in allData" :key="index">
          <el-descriptions
            v-if="one.title !== '车辆信息' && one.title !== '房产信息'&& one.title !== '借款信息'&& one.title !== '申请信息' && one.title !== '驾驶证信息'"
            :title="one.title" name="1" size="medium" :column="3" class="con"> <template slot="title">
              <p class="title">{{ one.title }}</p>
            </template>
            <el-descriptions-item v-for="(oneone, index1) in one.viewDetails" :key="index1" :label="oneone.fieldName">
              <div v-if="oneone.fieldType != 'pictrue'">
                <span v-text="oneone.value" />
              </div>
              <div v-else-if="oneone.fieldType == 'pictrue'">
                <el-image style="width: 160px; height: 90px"
                  :src="require('@/views/production/collaborativeProduct/1561390725417.jpeg')" />
              </div>
            </el-descriptions-item>
          </el-descriptions>
          <hr
            v-if="one.title !== '车辆信息' && one.title !== '房产信息'&& one.title !== '借款信息'&& one.title !== '申请信息'&& one.title !== '驾驶证信息'">
        </div>
      </el-col>
    </el-row>
    <el-row justify="center" type="flex" class="btn">
      <el-col :span="6" :offset="18">
        <el-button type="primary" @click="editPrincipal">编辑负责人</el-button>
      </el-col>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :rules="rules" ref="ruleForm">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="选择负责人：" prop="principal">
          <el-select v-model="form.principal" placeholder="admin" clearable style="width: 260px">
            <el-option v-for="item in principalOPtions" :key="item.id" :label="item.name" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编辑备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  <div app-container v-else-if="this.handle=== 'update' " name="update">
    <el-row type="flex" justify="center">
      <el-col :span="21">
        <div align="center" style="background-color: lightgray;height: 60px;">
          <div>
            <span id="mainTitle">订单详情</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row class="mb8" justify="center" type="flex">
      <el-col :span="20">
        <el-descriptions title="申请信息" name="1" size="medium" :column="3" class="con">
          <el-descriptions-item label="申请编号">
            <span v-text="info.orderNumber" />
          </el-descriptions-item>
          <el-descriptions-item label="申请状态">
            <span v-text="info.applicantStatus" />
          </el-descriptions-item>
          <el-descriptions-item label="产品渠道名">
            <span v-text="info.proChan" />
          </el-descriptions-item>
          <el-descriptions-item label="客户姓名">
            <span v-text="info.clientName" />
          </el-descriptions-item>
          <el-descriptions-item label="手机号码">
            <span v-text="info.phone" />
          </el-descriptions-item>
          <el-descriptions-item label="身份证号">
            <span v-text="info.citizenIdentity" />
          </el-descriptions-item>
          <el-descriptions-item label="渠道">
            <span v-text="info.channelName" />
          </el-descriptions-item>
        </el-descriptions>
        <hr>
      </el-col>
    </el-row>
    <el-row class="mb8" justify="center" type="flex">
      <el-col :span="20">
        <el-descriptions title="借款信息" name="1" size="medium" :column="3" class="con">
          <template slot="title">
            <p class="title">借款信息</p>
          </template>
          <el-descriptions-item label="贷款金额">
            <span v-text="info.loanAmount" />
          </el-descriptions-item>
          <el-descriptions-item label="还款方式">
            <span v-text="info.paymentStatusValue" />
          </el-descriptions-item>
          <el-descriptions-item label="分期期数">
            <span v-text="info.periods" />
          </el-descriptions-item>
          <el-descriptions-item label="贷款用途">
            <span v-text="info.usage" />
          </el-descriptions-item>
          <el-descriptions-item label="还款来源">
            <span v-text="info.paybackOrigin" />
          </el-descriptions-item>
          <el-descriptions-item label="还款保障">
            <span v-text="info.paybackGuarantee" />
          </el-descriptions-item>
        </el-descriptions>
        <hr>
      </el-col>
    </el-row>
    <el-collapse class="dataBox" v-for="(one, index) in allData" v-model="activeNames" @change="handleChange">

      <el-collapse-item :title="one.title" name="1">
        <template slot="title">
          <p class="title">{{ one.title }}</p>
        </template>
        <div v-for="oneone in one.viewDetails" id="context">
          <el-form :ref="oneone" :model="oneone" label-width="auto" label-position="left" :inline="true">
            <el-form-item v-if="oneone.fieldType != 'select'" :label="oneone.fieldName" :prop="oneone.field">
              <div v-if="oneone.fieldName.includes('正面')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess0" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl0" :src="imageUrl0" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else-if="oneone.fieldName.includes('反面')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess1" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else-if="oneone.fieldName.includes('手持')">

                <el-upload class="avatar-uploader" action="http://localhost:8080/feed/imgUpload" :show-file-list="false"
                  :on-success="handleAvatarSuccess2" :before-upload="beforeAvatarUpload" :auto-upload="true"
                  :headers="uploadHeaders">
                  <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
              <div v-else>
                <el-input v-model="oneone.value" :type="oneone.fieldType" :value="oneone.value"
                  :class="oneone.fieldClass" />
              </div>

            </el-form-item>

            <el-form-item v-else-if="oneone.fieldType == 'select'" :label="oneone.fieldName" :prop="oneone.field">
              <el-select v-model="oneone.value" placeholder="">
                <el-option v-for="dict in dict.type.user_personal_national"
                  v-if="oneone.dictType == 'dict.type.user_personal_national'" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>

          </el-form>
        </div>
      </el-collapse-item>

    </el-collapse>
    <el-row :gutter="10" class="btn">

      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="commit" plain size="max" @click="saveUerInfo($event)">
          提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="save" plain size="max" @click="saveUerInfo($event)">
          保存但不提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:post:add']" type="primary" name="giveUp" plain size="max" @click="saveUerInfo($event)">
          放弃申请</el-button>
      </el-col>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :rules="rules" ref="ruleForm">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="选择负责人：" prop="principal">
          <el-select v-model="form.principal" placeholder="admin" clearable style="width: 260px">
            <el-option v-for="item in principalOPtions" :key="item.id" :label="item.name" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编辑备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>

// import {findUser} from '@/api/client'
import { dataRenering , newViewDetails } from '@/api/viewDetails';
import { getFeed, findPrincipal, UpdateFeedPrincipal,saveInfoToClientTable } from '@/api/applicat'
import Cookies from "js-cookie"; export default {

  dicts: ['user_personal_national', 'user_personal_marital_status'],
  data() {

    return {
      uploadHeaders: {
        "token": Cookies.get('Admin-Token'),
        "Content-Type": "multipart/form-data"
      },

      title: '',
      feedId: undefined,
      open: false,

      imageUrl0: '',
      imageUrl1: '',
      imageUrl2: '',
      rules: {
        principal: [
          { required: true, message: "负责人不能为空", trigger: "change" }
        ],

      },
      ruleForm: {
        principal: '',
        remark: ''
      },
      form: [
        {
          id: undefined,
          principal: undefined,
          remark: undefined

        }],
      principalOPtions: [],
      newFeedLoanNum: undefined,
      clientId: undefined,
      feedId: undefined,
      newFeedProductName:undefined,
      handle: undefined,
      optFlag:undefined,
      allData: [
        {
          title: '个人信息',
          data: []
        }
      ],
      saveData: {
        clientId: undefined,
        allData: []
      },

      info: [],
      pageInfo: [
        {
          name: undefined,
        }
      ],
      userInfo: [],
      activeNames: ['1'],
      queryParams: {
        clientName: undefined,
        citizenIdentity: undefined,
        phone: undefined,

        channelName: undefined,
        productionName: undefined,
      },
    }
  },
  created() {

    this.handle = this.$route.query.handle

    if (this.handle === 'viewDetails' || this.handle === 'update') {
      this.feedId = this.$route.query.feedId
      this.clientId=this.$route.query.clientId
     
      this.getFeedDetails()
    } else if (this.handle === 'add2') {

      this.queryParams = this.$route.query.baseParams
      this.clientId = this.$route.query.newFeedClientId

      this.feedId = this.$route.query.newFeedLoanId

      this.newFeedLoanNum = this.$route.query.newFeedLoanNum
      this.newFeedProductName=this.$route.query.newFeedProductName
   
      this.getAddNewFeed2()
    }




  },
  methods: {
    getAddNewFeed2() {
      dataRenering().then(res => {
        this.allData = [];
        //按照下列顺序渲染      
        for (let i = 0; i < res.data.length; ++i) {
          if (res.data[i].title === '借款信息') {
            this.allData[i] = res.data[i];
          } else if (res.data[i].title === '个人信息') {
            this.allData[i] = res.data[i];
          } else if (res.data[i].title === '职业信息') {
            this.allData[i] = res.data[i];
          } else if (res.data[i].title === '家庭经济') {
            this.allData[i] = res.data[i];
          } else if (res.data[i].title === '联系人信息') {

            this.allData[i] = res.data[i];
          }
        }

        this.allData = res.data
      }).catch();

    },
    getFeedDetails() {
     
      newViewDetails(this.clientId).then(res => {
        this.allData = res.data

      }).catch()
      getFeed(this.feedId).then(res => {
        this.info = res.data
      }).catch()
      findPrincipal().then(
        res => {
          this.principalOPtions = res.data
        }
      ).catch()
    },
    saveUerInfo(button) {
 
     let optName=button.target.getAttribute('name')
     console.log(this.clientId)
      saveInfoToClientTable(this.clientId, this.allData,'commit').then(
        res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("成功");
            this.getFeedDetails(this.clientId);
            this.$router.go(-2)
              this.$store.state.tagsView.visitedViews.splice(this.$store.state.tagsView.visitedViews.findIndex(item => item.path === this.$route.path), 1)
              this.$router.push(this.$store.state.tagsView.visitedViews[this.$store.state.tagsView.visitedViews.length - 2].path)
          }
        }
      ).catch()
    },
    selectPrincipal() {

    },
    editPrincipal() {

      this.reset()
      this.open = true
      this.title = '编辑负责人'

    },
    reset() {
      this.form = {
        id: undefined,
        principal: undefined,
        remark: undefined,

      }
      this.resetForm('form')
    },

    handleChange(val) {
      console.log(val)
      console.log(this.allData)
    },
    handleAvatarSuccess0(res, file) {

      this.imageUrl0 = URL.createObjectURL(file.raw);
    },
    handleAvatarSuccess1(res, file) {

      this.imageUrl1 = URL.createObjectURL(file.raw);
    },
    handleAvatarSuccess2(res, file) {

      this.imageUrl2 = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!');
      }
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 5MB!');
      }
      return isJPG && isLt5M;
    },
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.id = this.feedId
          if (this.form.id !== undefined) {

            UpdateFeedPrincipal(this.form).then(response => {
              this.$modal.msgSuccess('成功')
              this.open = false

            })
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.reset()
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

body>.el-container {
  margin: auto;
  margin-bottom: 40px;
  display: inline-block;
}

.dataBox {
  width: 90%;
  margin: auto;
}

/* 调整整个名字和输入框占的盒子大小 */
#context {
  float: left;
  /* width: 20%;  */
  margin-left: 50px;
}

.long {
  width: 1050px;
  /* margin-left: 50px; */
}

.short {
  /* float: left; */
  width: 450px;
  /* margin-left: 50px; */
}

.title {
  font-size: 18px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #303133;
  padding-bottom: 17px;
  padding-top: 17px;
}

.btn {
  height: 40px;
  position: fixed;
  bottom: 0;
  padding: 8px;
  text-align: right;

}
</style>